package com.blb.servlet;

import com.blb.pojo.Comment;
import com.blb.pojo.Topic;
import com.blb.pojo.User;
import com.blb.service.ICommentService;
import com.blb.service.ITopicService;
import com.blb.service.IUserService;
import com.blb.service.impl.CommentServiceImpl;
import com.blb.service.impl.TopicServiceImpl;
import com.blb.service.impl.UserServiceImpl;
import com.blb.utils.SpringUtils;
import com.blb.utils.UUIDUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;
@WebServlet("/topic/*")


public class TopicServlet extends BaseServlet{
    private ITopicService topicService = SpringUtils.getBean(ITopicService.class);
    private IUserService userService = SpringUtils.getBean(IUserService.class);
    private ICommentService commentService = SpringUtils.getBean(ICommentService.class);

    /**
     * 添加朋友圈
     * @param resp
     * @param req
     */
    public void add(HttpServletRequest req,HttpServletResponse resp ) throws IOException {
        String content = req.getParameter("content");
        String image = req.getParameter("image");

        User user = (User) req.getSession().getAttribute("user");

        Topic topic = new Topic(UUIDUtils.getId(),content,image, LocalDateTime.now(),user.getId());
        try {
            topicService.add(topic);
            resp.getWriter().write("ok");
        } catch (SQLException | IOException e) {
            e.printStackTrace();
            resp.getWriter().write("保存失败：" + e.getMessage());
        }
    }

    /**
     * 删除
     * @param req
     * @param resp
     */
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");

        try {
            topicService.delete(id);
            resp.getWriter().write("ok");
        } catch (SQLException e) {
            e.printStackTrace();
            resp.getWriter().write("删除失败：" + e.getMessage());
        }
    }

    /**
     * 朋友圈列表
     * @param req
     * @param resp
     */
    public void list(HttpServletRequest req, HttpServletResponse resp){

        try {
            User user = (User) req.getSession().getAttribute("user");
            req.setAttribute("user", user);
            List<Comment> commentList = commentService.list();
            req.setAttribute("commentList", commentList);
            //获取话题列表
            List<Topic> topicList = topicService.list();
            req.setAttribute("topicList",topicList);
            //获取用户列表
            List<User> userList = userService.list();
            req.setAttribute("userList",userList);
            req.getRequestDispatcher("/moments.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("msg",e.getMessage());
            throw new RuntimeException(e);
        }

    }
}
